package cn.nextProject.controller;

import cn.nextProject.entity.Result;
import cn.nextProject.entity.RoleGroupPermission;
import cn.nextProject.entity.req.PermissionReq;
import cn.nextProject.service.PermissionService;
import cn.nextProject.service.RoleGroupPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class RoleGroupPermissionController {

    @Autowired
    private RoleGroupPermissionService roleGroupPermissionService;

    @GetMapping("/findRoleGroupPermission")
    public Result<?> findRoleGroupPermission(@RequestParam("roleGroupId") Long roleGroupId,
                                             @RequestParam("path")String path){

        if (ObjectUtils.isEmpty(roleGroupId)){
            return new Result<>().fail("参数不能为空");
        }

        List<RoleGroupPermission> byRoleGroup = roleGroupPermissionService.findByRoleGroup(roleGroupId, path);

        return new Result<>().success("查询成功",byRoleGroup);

    }

    @PostMapping("/saveBatchPermission")
    public Result<?> saveBatchPermission(@RequestBody PermissionReq permissionReq){

        if (ObjectUtils.isEmpty(permissionReq)){
            return new Result<>().fail("参数不能为空");
        }
        Long roleGroupId = permissionReq.getRoleGroupId();
        List<Long> permissionIdList = permissionReq.getPermissionIdList();

        if (ObjectUtils.isEmpty(roleGroupId)){
            return new Result<>().fail("请选择要添加权限的角色组");
        }

        if (CollectionUtils.isEmpty(permissionIdList)){
            return new Result<>().fail("请选择要添加的权限");
        }

        roleGroupPermissionService.saveBatch(roleGroupId, permissionIdList);

        return new Result<>().success("添加成功", null);

    }

    @GetMapping("/deleteRoleGroupPermission")
    public Result<?> deleteRoleGroupPermission(@RequestParam("id") Long id){

        if (ObjectUtils.isEmpty(id)){
            return new Result<>().fail("参数不能为空");
        }

        roleGroupPermissionService.deleteRoleGroupPermission(id);

        return new Result<>().success("删除成功", null);

    }

}
